/**
 * 销售报表统计
 */
var queryStatisticSaleSearch = function() {
	var querystatisticSaleFieldSet = new Ext.form.FieldSet({
				id : 'querystatisticSaleFieldSet',
				title : '查询条件',
				collapsible : true,// 可折叠
				buttonAlign : 'center',
				keys : [{
							key : 13, // 13代表回车
							fn : queryStatisticSale,
							scope : this
						}],
				items : [{
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : '年份',
									hiddenName : 'year',
									loaclarr : [['2015',2015],['2016',2016], ['2017',2017],['2018',2018], ['2019',2019],['2020',2020], ['2021',2021]
									,['2022',2022], ['2023',2023],['2024',2024]]
								}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : '月份',
									hiddenName : 'month',
									loaclarr : [['01',01],['02',02], ['03',03],['04',04], ['05',05],['06',06], ['07',07]
									,['08',08], ['09',09],['10',10], ['11',11],['12',12],['','全部']]
								}]
							},{
						columnWidth : .30,
						layout : 'form',
						border : false,
						items:[{
							layout : 'column',
							border : false,
							items:[{
												text : '查询',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryStatisticSale();
												},
												style : {
													marginLeft : "100px",
													marginRight : "10px"
												}
											}, {
												text : '重置',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryStaticSaleForm.getForm().reset();
												}
											}]
						}]
					}]
				}]
			});
	fieldSetListeners(['querystatisticSaleFieldSet', 'queryStaticSaleForm',
					'statisticSaleManagerGrid'], [0.12, 0.88]);
	var queryStaticSaleForm = new Ext.form.FormPanel({
				id : 'queryStaticSaleForm',
				anchor : '100% 12%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [querystatisticSaleFieldSet]
			});
	return queryStaticSaleForm;
};

/**
 * grids
 */
var statisticSaleManagerGrid = function() {
	// 定义表的每列（对应表头信息）
	var statisticFileManagerCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				header : "商品名称",
				width : 80,
				sortable : true,
				dataIndex : 'goodsName'
			}, {
				header : "规格",
				width : 80,
				sortable : true,
				dataIndex : 'goodsWeight',
				renderer:function(h,g,c,d){
					var a = c.data.goodsName;
					var g = c.data.couponType;
					var d = c.data.goodsWeight;
					if(a == "线下抵用券"){
						return PmsUtil.CouponTypeSlip(g);
					}
					return d;
				}
			}, {
				header : "数量",
				width : 80,
				sortable : true,
				dataIndex : 'goodsNumber'
			}, {
				header : "单价",
				width : 80,
				sortable : true,
				dataIndex : 'goodsPrice'
			},{
				header : "实际付款",
				width : 80,
				sortable : true,
				dataIndex : 'totalAmount'
			}, {
				header : "创建日期",
				width : 80,
				hiddern : true,
				sortable : true,
				dataIndex : 'createDate',
				renderer : function(date) {
					return Ext.util.Format.substr(date, 0, 10);
				}
			}]);
			
	
	// 定义grid的toolbar
	var statisticSaleTb = new Ext.Toolbar({
				id : "statisticSaleTb",
				height : 25,
				items : []
			});
	statisticSaleTb.add(new Ext.Toolbar.Spacer());
	statisticSaleTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'statisticSale_刷新',
				pressed : true,
				handler : function() {
					queryStatisticSale();
				}
			}));

	var statisticSaleManagerReader = new Ext.data.JsonReader({
				root : 'list',
				fields : [{
							name : 'id'
						}, {
							name : 'goodsNumber'
						}, {
							name : 'goodsName'
						}, {
							name : 'goodsPrice'
						}, {
							name : 'goodsWeight'
						}, {
							name : 'createDate'
						}, {
							name : 'couponType'
						}, {
							name : 'totalAmount'
						}]
			});

	// 取数据store
	var statisticSaleViewStore = new Ext.data.GroupingStore({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/statisticManager/selectSatasticOrderSale"
						}),
				groupField : 'createDate',
				sortInfo : {
					field : 'createDate',
					direction : 'ASC'
				},
				groupOnSort : true,
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryStaticSaleForm')
											.getForm().getValues());
						}
					}
				},
				reader : statisticSaleManagerReader
			});

	var statsticFileView = new Ext.grid.GroupingView({
		forceFit : true,
		startCollapsed : false,
		hideGroupedColumn : true,
		collapsible : true,
		animCollapse : true,
		sortAscText : "升序",
		sortDescText : "降序",
		columnsText : "表格字段",
		groupByText : "使用当前字段进行分组",
		showGroupsText : "表格分组"
		
	});

	var statisticFileManagerGrid = new Ext.grid.GridPanel({
				id : 'statisticFileManagerGrid',
				store : statisticSaleViewStore,
				cm : statisticFileManagerCm,
				view : statsticFileView,
				collapsible : true,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				animCollapse : false,
				title : '销售报表统计',
				iconCls : 'icon-grid',
				tbar:statisticSaleTb,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 88%',
				frame : true
			});
	return statisticFileManagerGrid;
};

// 查询
function queryStatisticSale() {
	var grid = Ext.getCmp("statisticFileManagerGrid");
	var form = Ext.getCmp("queryStaticSaleForm");
	if (form.getForm().isValid()) {
		var gridStore = grid.getStore();
		gridStore.baseParams = Ext.apply(gridStore.baseParams, form.getForm()
						.getValues());
		gridStore.load();
	}
}
/**
 * 计算商品总数
 * 
 * @param {}
 *            groupRecod
 * @return {}
 */
function getGoodsNumberSum(groupRecod) {
	var goodsSum = 0;
	for (var i = 0; i < groupRecod.rs.length; i++) {
		var recode = groupRecod.rs[i].data;
		if (recode.goodsType == 0) {
			goodsSum += recode.goodsNumber;
		}
	}
	return goodsSum;
};
/**
 * 计算配件总数
 * 
 * @param {}
 *            groupRecod
 * @return {}
 */
function getGoodsFixingNumberSum(groupRecod) {
	var goodsSum = 0;
	for (var i = 0; i < groupRecod.rs.length; i++) {
		var recode = groupRecod.rs[i].data;
		if (recode.goodsType == 1) {
			goodsSum += recode.goodsNumber;
		}
	}
	return goodsSum;
};
/**
 * 时间格式转换
 * 
 * @param {}
 *            createDate
 * @return {}
 */
function transOrderDeliveryDate(createDate) {
	var date = createDate.substring(0, 10);
	var arr = [];
	arr = date.split("-");
	var str = arr.join("");// str = "20131111";
	return str;
}
/**
 * 导出Excel
 */
function exportStatisticsOrder(createDate) {
	var date_str = createDate.toString().replace(/(\d{4})(\d{2})(\d{2})/g,'$1-$2-$3');
	document.location.href = (__ctxPath
			+ "/statisticManager/exportStatisticsOrder?createDate=" + date_str);
};
Ext.getCmp("centerTabPanel").getActiveTab().add(queryStatisticSaleSearch(),
		statisticSaleManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
